import 'dart:io';

import 'package:flutter/material.dart';
import 'package:word_range_client/viewModels/EventViewModel.dart';
import 'package:word_range_client/widgets/general/Toastification.dart';

import '../../pojo/vo/EventVo.dart';
import '../../widgets/general/EventItems.dart';
import '../event/SilentTest.dart';

class EventPageView extends StatelessWidget {
  const EventPageView({super.key});

  @override
  Widget build(BuildContext context) {
    if(Platform.isAndroid){
      return Scaffold(
        appBar: AppBar(
          title: const Text("活动"),
        ),
        body: const _EventPageViewBody(),
        floatingActionButton:SizedBox(
          width: 55,
          height: 55,
          child: FloatingActionButton(
            onPressed: (){
              // 刷新逻辑
              Toastification toastification = Toastification(context: context);
              toastification.showSuccess("活动信息刷新成功");
              // 触发页面重新加载
              Navigator.pushReplacement(
                context,
                MaterialPageRoute(
                  builder: (context) => const EventPageView(),
                ),
              );
            },
            child: Icon(
              Icons.refresh,
              size: 20,
              color: Theme.of(context).colorScheme.background,
            ),
          ),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
      );
    }
    return const Scaffold(
      body: _EventPageViewBody(),
    );
  }
}

class _EventPageViewBody extends StatefulWidget {
  const _EventPageViewBody({super.key});

  @override
  State<_EventPageViewBody> createState() => _EventPageViewBodyState();
}

class _EventPageViewBodyState extends State<_EventPageViewBody> {
  Future<List<Widget>> _initItems() async {
    List<Widget> tempList = [];
    List<EventVo> allEvent = await EventViewModel.getAllEvent();
    EventVo eventVo = EventVo(title: "单词默写抽查", subTitle: "日常活动，积分多多", imageUrl: "https://www.itying.com/images/flutter/7.png");
    tempList.add(
        EventItem(eventVo: eventVo,onTap: _toSilentTestPage,)
    );
    if (allEvent.isEmpty) {
      // 显示错误信息
      tempList.add(
          const Center(
            child: Text("暂无活动信息"),
          )
      );
    }
    for(var i = 0; i < allEvent.length; i++){
      EventVo eventVo = allEvent[i];
      tempList.add(
          EventItem(eventVo: eventVo)
      );
    }

    return tempList;
  }

  void _toSilentTestPage(){
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => const SilentTest(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Widget>>(
      future: _initItems(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return Center(child: Text('加载失败: ${snapshot.error}'));
          }
          return GridView.extent(
            maxCrossAxisExtent: 600,
            childAspectRatio: 1.74 / 1,
            mainAxisSpacing: 15,
            crossAxisSpacing: 15,
            padding: const EdgeInsets.all(15),
            children: snapshot.data!,
          );
        }
        return const Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                CircularProgressIndicator(),
                Text("加载中。。。")
              ],
            )
        );
      },
    );
  }
}


